﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GridRegulator
{
    public static class ImageEvaluator
    {
        public static double ImageComplexity(GrayImage gray, int y, int x, int h, int w)
        {
            double mean = 0;
            double complexity = 0;
            for (int i = y; i < y + h; i++)
            {
                for (int j = x; j < x + w; j++)
                {
                    mean += gray[i, j];
                }
            }
            mean /= w * h;

            for (int i = y; i < y + h; i++)
            {
                for (int j = x; j < x + w; j++)
                {
                    double d = gray[i, j] - mean;
                    d *= d;
                    complexity += d;
                }
            }

            complexity /= w * h;
            complexity = Math.Sqrt(complexity);

            return complexity;
        }

        public static double ImageSAD(GrayImage gray, int y0, int x0, int h, int w, int y1, int x1, int v)
        {
            double s = 0;
            int y1end = y1 + h;
            int x1end = x1 + w;
            for (int i0 = y0, i1 = y1; i1 < y1end; i0++, i1++)
            {
                for (int j0 = x0, j1 = x1; j1 < x1end; j0++, j1++)
                {
                    int ivbegin = i1 - v;
                    int ivend = i1 + v + 1;
                    int jvbegin = j1 - v;
                    int jvend = j1 + v + 1;

                    double dmin = double.MaxValue;
                    for (int iv = ivbegin; iv < ivend; iv++)
                    {
                        for (int jv = jvbegin; jv < jvend; jv++)
                        {
                            double d = gray[i0, j0] - gray[iv, jv];
                            d = Math.Abs(d);
                            if (d < dmin)
                            {
                                dmin = d;
                            }
                        }
                    }
                   
                    s += dmin;
                }
            }
            s /= w * h;
            return s;
        }

        public static bool HardCompare(GrayImage gray, int y0, int x0, int h, int w, 
            int y1, int x1, int v, int thr)
        {
            int y1end = y1 + h;
            int x1end = x1 + w;
            for (int i0 = y0, i1 = y1; i1 < y1end; i0++, i1++)
            {
                for (int j0 = x0, j1 = x1; j1 < x1end; j0++, j1++)
                {
                    int ivbegin = i1 - v;
                    int ivend = i1 + v + 1;
                    int jvbegin = j1 - v;
                    int jvend = j1 + v + 1;

                    double d = gray[i0, j0] - gray[i1, j1];
                    d = Math.Abs(d);
                    if (d < thr)
                        continue;

                    bool found = false;
                    for (int iv = ivbegin; iv < ivend && !found; iv++)
                    {
                        for (int jv = jvbegin; jv < jvend; jv++)
                        {
                            d = gray[i0, j0] - gray[iv, jv];
                            d = Math.Abs(d);
                            if (d < thr)
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    if (!found)
                        return false;
                }
            }
            return true;
        }
    }
}
